/**************************************************************
 *@ProjectName: blweibo
 *@FileName: UserListItemView.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhiweibo.models.User;
import com.zhiweibo.util.HttpUtil;

/**
 * The Class UserListItemView.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
class UserListItemView extends LinearLayout {

    /** The m user. */
    private User mUser;

    /** The m nick. */
    private final TextView mNick;

    /** The m num. */
    private final TextView mNum;

    /** The m portrait. */
    private final ImageView mPortrait;

    /** The task1. */
    private AsyncTask task1;

    /**
     * Instantiates a new user list item view.
     * 
     * @param paramContext
     *            the param context
     * @param user
     *            the user
     */
    public UserListItemView(Context paramContext, User user) {
        super(paramContext);
        mUser = user;
        ((LayoutInflater) paramContext.getSystemService("layout_inflater"))
                .inflate(R.layout.userinfolistitemview, this);
        mNick = (TextView) findViewById(R.id.tvNick);
        mPortrait = (ImageView) findViewById(R.id.ivPortrait);
        mNum = (TextView) findViewById(R.id.tvItemNum);
        setUI();
    }

    /**
     * Sets the ui.
     */
    public void setUI() {
        mNick.setText(mUser.getUsername());
        mNum.setText(mUser.getFans_count());
        task1 = new FetchImage(mPortrait, mUser.getFace()).execute(0);
    }

    /**
     * Update.
     * 
     * @param paramContext
     *            the param context
     * @param user
     *            the user
     */
    public void update(Context paramContext, User user) {
        mUser = user;
        mPortrait.setImageResource(R.drawable.portrait);
        if (task1 != null && task1.getStatus() == AsyncTask.Status.RUNNING) {
            task1.cancel(true);
        }
        setUI();
    }

    /**
     * The Class FetchImage.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class FetchImage extends AsyncTask {

        /** The piv. */
        private ImageView piv;

        /** The str url. */
        private String strUrl;

        /** The bm. */
        private Bitmap bm;

        /**
         * Instantiates a new fetch image.
         * 
         * @param iv
         *            the iv
         * @param url
         *            the url
         */
        public FetchImage(ImageView iv, String url) {
            piv = iv;
            strUrl = url;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            bm = HttpUtil.getImage(getContext(), strUrl);
            return bm;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            piv.setImageBitmap(bm);
        }

    }
}